const puppeteer = require('puppeteer');

(async () => {
  const browser = await puppeteer.launch();
  const page = await browser.newPage();
  await page.goto('https://webmail.vip.188.com');
  //注册一个 Node.js 函数，在浏览器里运行
  await page.exposeFunction('md5', text =>
    crypto.createHash('md5').update(text).digest('hex')
  );
  //通过 page.evaluate 在浏览器里执行删除无用的 iframe 代码
  await page.evaluate(async () =>  {
    let iframes = document.getElementsByTagName('iframe');
    for(let i = 3; i <  iframes.length - 1; i++){
      let iframe = iframes[i];
      if(iframe.name.includes("frameBody")){
        iframe.src = 'about:blank';
        try{
          iframe.contentWindow.document.write('');
          iframe.contentWindow.document.clear();
        }catch(e){}
        //把iframe从页面移除
        iframe.parentNode.removeChild(iframe);
      }
    }
    //在页面中调用 Node.js 环境中的函数
    const myHash = await window.md5('PUPPETEER');
    console.log(`md5 of ${myString} is ${myHash}`);
  });
  await page.close();
  await browser.close();
})();
